﻿package  
{
	/**
	 * ...
	 * @author 
	 */
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.getTimer;
	
	import cn.rayyee.loader.ReadXML;
	import IAvatar.IView;
	
	import away3d.containers.View3D;
	import away3d.loaders.Collada;
	import away3d.core.render.Renderer;
	import away3d.cameras.Camera3D;
	import away3d.core.math.Number3D;
	import away3d.lights.DirectionalLight3D;
	import away3d.core.base.Object3D;
	import away3d.containers.ObjectContainer3D;
	import away3d.animators.SkinAnimation;
	import away3d.loaders.data.AnimationData;
	import away3d.loaders.data.MaterialData;
	import away3d.core.utils.Cast;
	public class AvatarView extends Sprite implements IView
	{
		//定义所有的材质
		private var materialface:MaterialData;
		private var materialcoat:MaterialData;
		private var materialcoatl:MaterialData;
		private var materialhair:MaterialData;
		private var materialpants:MaterialData;
		private var materialdress:MaterialData;
		private var materialdressl:MaterialData;
		private var materialh:MaterialData;
		private var materialhead:MaterialData;
		
		private var _view:View3D;
		private var _light:DirectionalLight3D;
		private var target:Object3D;
		
		[Embed(source="pifu.jpg")] 
		private var minpic : Class; 
		[Embed(source="tex_Bcoat.jpg")] 
		private var coatbmp : Class; 
		[Embed(source="tex_Bdress.jpg")] 
		private var dressbmp : Class; 
		[Embed(source="tex_Bhead.jpg")] 
		private var headbmp : Class; 
		[Embed(source="tex_Bpants.jpg")] 
		private var pantsbmp : Class; 
		[Embed(source="tex_hair.jpg")] 
		private var hairbmp : Class; 
		
		private var min:MovieClip = new MovieClip();
		private var basic:ObjectContainer3D;//3d模型
		//动画
		private var runObject:ObjectContainer3D;
		private var skinAnimationrun:SkinAnimation;
		private var basicAnimation:SkinAnimation;
		public function AvatarView() 
		{
			initView();
			ReadXML.readInetAddress("assest/basic.xml", _callBack, _error);
			function _callBack(xmldata:XML):void {
				initObject(xmldata);
			}
			function _error(e:Event):void {trace(e)}
		}
		//执行动画的刷新播放
		private function onRunHandler(event:Event):void {
			//update
			_view.render();
			basic.rotationY++;
			_view.scene.rotationY ++;
			basicAnimation.update(getTimer()/1500);
		}
		/**
         *
         * 初始化3d模型对象
         * @return          void
         * 
         */ 
		public function initObject(xmldata:XML):void {
			ReadXML.readInetAddress("assest/run.xml", _callBack, _error);
			function _callBack(xmldata:XML):void {
				runObject = Collada.parse(xmldata, { material:min } );
				skinAnimationrun = (runObject.animationLibrary["default"] as AnimationData).animation as SkinAnimation;
				basicAnimation = skinAnimationrun.clone(basic) as SkinAnimation;
				addEventListener(Event.ENTER_FRAME, onRunHandler);
				trace("ko")
			}
			function _error(e:Event):void {trace(e)}
			// 解析xml模型文件
			basic = Collada.parse(xmldata, { material:min } );
			//加载到3d视口并调整相机
			_view.scene.addChild(basic);
			_view.camera.lookAt(basic.position);
			initMaterials();
			
		}
		/**
         *
         * 初始化3d场景
         * @return          void
         * 
         */ 
		private function initView():void {
			//view3D
			_view = new View3D({x:800 *.5, y:600 * .5-40, camera: new Camera3D(), renderer:Renderer.BASIC});
			//light
			_light = new DirectionalLight3D();
			_light.specular = 0;
			_light.ambient = 0.5;
			_light.diffuse = 1;
			_view.scene.addChild(_light);
			//_light.position = new Number3D(-10000000, 900000, -8400000);
			//addChild(view)
			target = new Object3D();
			_view.scene.addChild(target);
			_view.camera.lookAt(target.position);
			//view.addEventListener(Event.ADDED_TO_STAGE, init);
			addChild(_view);
		}
		/**
         *
         * 初始化贴图对象
         * @return          void
         * 
         */ 
		private function initMaterials():void {
			// 读取材质material
			materialface = basic.materialLibrary.getMaterial("mat_Bhead");
			materialcoat= basic.materialLibrary.getMaterial("mat_Bcoat");
			//materialcoatl= basic.materialLibrary.getMaterial("mat_BLcoat");
			materialh= basic.materialLibrary.getMaterial("mat_Bhair");
			materialpants= basic.materialLibrary.getMaterial("mat_Bpants");
			materialdress= basic.materialLibrary.getMaterial("mat_Bdress");
			//materialdressl= basic.materialLibrary.getMaterial("mat_BLdress");
			materialface.material = Cast.material(headbmp);
			materialcoat.material = Cast.material(coatbmp);
			//materialcoatl.material = Cast.material(minpic);
			//materialdressl.material = Cast.material(minpic);
			materialdress.material = Cast.material(dressbmp);
			materialh.material = Cast.material(hairbmp);
			materialpants.material = Cast.material(pantsbmp);
		}
	}

}